﻿using System.IO;
using LoungeRepo.Contracts.EntityStore;

namespace LoungeRepo.EntityStore
{
    public partial class FilesystemEntityStore : IEntityStore
    {
        private readonly string rootFolder;

        public FilesystemEntityStore(string rootFolder)
        {
            this.rootFolder = rootFolder;
        }


        public void Store(IStreamedEntity streamedEntity)
        {
            using(var target = CreateEntityFile(streamedEntity))
            {
                Store(target, streamedEntity);
            }
        }


        public IStreamedEntity Load(string id, string partition)
        {
            using(var source = new FileStream(CreateEntityFilename(id, partition), FileMode.Open))
            {
                IStreamedEntity se = Load(id, partition, source);
                se.Blob.Seek(0, SeekOrigin.Begin);
                return se;
            }
        }


        public void Delete(string id, string partition)
        {
            var filename = CreateEntityFilename(id, partition);
            File.Delete(filename);
        }
    }
}
